package com.tadpole.music.presenter.home;

import com.tadpole.music.bean.home.ClockInRankBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.home.ClockInRankIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取打卡榜单
 * Created By 孙岩
 * on 2020/12/10
 */
public class ClockInRankPresenter extends BasePresenter<ClockInRankIView> {
    @SuppressWarnings("unchecked")
    public void getClockInRank(){
        if(isViewAttached()){
            String url = UrlConfig.getClockInRank;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN,"");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    List<ClockInRankBean.DataBean.ListBean> listBeans = new ArrayList<>();
                                    JSONArray array = new JSONArray(object.getString("list"));
                                    for(int i=0;i<array.length();i++){
                                        JSONObject jsonObject = new JSONObject(array.get(i).toString());

                                        JSONObject signObj = new JSONObject(jsonObject.getString("sign"));

                                        JSONObject userObj = new JSONObject(signObj.getString("user"));

                                        ClockInRankBean.DataBean.ListBean.SignBean.UserBean userBean = new ClockInRankBean.DataBean.ListBean.SignBean.UserBean(
                                                JsonParseUtil.getInt(userObj,"id"),
                                                JsonParseUtil.getStr(userObj,"nickname"),
                                                JsonParseUtil.getStr(userObj,"avatar")
                                        );

                                        ClockInRankBean.DataBean.ListBean.SignBean signBean = new ClockInRankBean.DataBean.ListBean.SignBean(
                                                JsonParseUtil.getInt(signObj,"id"),
                                                JsonParseUtil.getInt(signObj,"user_id"),
                                                JsonParseUtil.getInt(signObj,"continue_sign_days"),
                                                JsonParseUtil.getInt(signObj,"total_sign_days"),
                                                userBean
                                        );

                                        ClockInRankBean.DataBean.ListBean listBean = new ClockInRankBean.DataBean.ListBean(
                                                JsonParseUtil.getBoolean(jsonObject,"is_me"),
                                                signBean
                                        );
                                        listBeans.add(listBean);
                                    }

                                    ClockInRankBean.DataBean dataBean = new ClockInRankBean.DataBean(
                                            JsonParseUtil.getInt(object,"rank"),
                                            listBeans
                                    );
                                    getView().showClockInRank(dataBean);
                                }else if(obj.getInt("code")==201){
                                    getView().showClockInRank(null);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
